<?php

namespace App\Admin\Controllers;

use App\Models\Article;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Models\AdminUser;

class ArticleController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('文章列表');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑文章');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {//
        return Admin::content(function (Content $content) {

            $content->header('创建文章');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Article::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->column('title','文章标题');
          //  $grid->column('user_id','所有者');
            $grid->user_id('创建者')->display(function($userId) {
                return AdminUser::find($userId)->username;
            });
            $grid->created_at();
            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Article::class, function (Form $form) {

            $form->display('id', 'ID');
            $user=Admin::user();
            //dd();
            $form->hidden('user_id')->default($user->id);
            $form->display('user_id')->default($user->id);
            $form->hidden('category_id')->value(2);
            $form->display('category_id')->value(2);
        /*    $form->user_id('用户')->display(function ($userId){
                return AdminUser::find($userId)->username;
            });*/
            $form->text('title', '文章标题');
            //$form->text('content', '文章内容');
            $form->editor('content');
           // $form->editor('content2');
           // dd(12222) ;
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
